package com.itheima.demo07PreparedStatement实现增删改;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/*
目标:使用PreparedStatement实现增删改

讲解:
	添加数据: 向user表添加1条记录

	注意:只有字段值不确定才能给?
 */
public class Demo07 {
    public static void main(String[] args) throws Exception {
        // 1.获取连接
        Connection conn = DriverManager.getConnection("jdbc:mysql:///day18", "root", "root");
        // 2.编写参数化SQL(带?的SQL语句)
        String sql = "INSERT INTO user VALUES (null, ?, ?);";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        // 3.给?赋值
        pstmt.setString(1,"xiaohong");
        pstmt.setString(2,"123");
        // 4.执行
        int row = pstmt.executeUpdate();
        System.out.println(row);
        // 5.释放资源
        pstmt.close();
        conn.close();
    }
}
